package com.ruoyi.location.service;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.config.PwdUtil;
import com.ruoyi.framework.shiro.token.JwtUtils;
import com.ruoyi.location.domain.location.LocationDomainService;
import com.ruoyi.location.domain.login.LoginDomainService;
import com.ruoyi.location.domain.team.TeamDomainService;
import com.ruoyi.location.domain.user.UserDomainService;
import com.ruoyi.location.exception.LocationRuntimeException;
import com.ruoyi.location.repository.LoginRepository;
import com.ruoyi.location.repository.generator.entity.LocationTeam;
import com.ruoyi.location.repository.generator.entity.SysUserGen;
import com.ruoyi.location.vo.req.*;
import com.ruoyi.location.vo.resp.UserLocationLatestResp;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AppService {

    @Autowired
    private LoginRepository loginRepository;
    @Autowired
    private LoginDomainService loginDomainService;
    @Autowired
    private LocationDomainService locationDomainService;
    @Autowired
    private TeamDomainService teamDomainService;
    @Autowired
    private UserDomainService userDomainService;
    @Autowired
    private JwtUtils jwtUtils;

    // login
    public void getMsgAuthCode(Long phoneNum) {
        loginDomainService.genCodeAndSend(phoneNum);
    }


    public Map<String, String> loginByPhoneNumAndMsgCode(LoginReq loginReq) {
        // 验证成功返回用户
        SysUserGen sysUser = loginDomainService.checkPhoneCode(loginReq.getPhoneNum(), loginReq.getMsgAuthCode());
        // 执行登录
        UsernamePasswordToken token = new UsernamePasswordToken(sysUser.getPhonenumber(), PwdUtil.decrypt(sysUser.getPassword()), true);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            throw new LocationRuntimeException(e);
        }
        // 上面登录成功设置 set-cookie
        // 下面返回token
        String token2 = jwtUtils.generateToken(sysUser.getUserId());

        // 3. 返回token
        Map<String, String> result = new HashMap<>();
        result.put("token", token2);
        return result;
    }

    public void locationReport(LocationReportReq reportReq) {
        locationDomainService.locationReport(reportReq);
    }

    // team
    public LocationTeam create(TeamCreateReq req) {
        return teamDomainService.create(req);
    }

    public LocationTeam update(TeamUpdateReq req) {
        return teamDomainService.update(req);
    }

    public void deleteTeam(Long teamId) {
        teamDomainService.delete(teamId);
    }
    public void quitTeam(Long teamId) {
        teamDomainService.quit(teamId);
    }

    public String createShareLink(Long teamId) {
        return teamDomainService.createShareLink(teamId);
    }

    public void joinTeam(Long teamId) {
        teamDomainService.joinTeam(teamId);
    }

    public List<UserLocationLatestResp> listLatestLocationByTeamId(Long teamId) {
        List<UserLocationLatestResp> result = teamDomainService.listLatestLocationByTeamId(teamId);
        return result;
    }

    // user
    public void updateUserInfo(UserNameUpdateReq req) {
        userDomainService.updateUserInfo(req);
    }
}
